package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/shoppingCart")
@Api(value = "购物车接口")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping("/add")
    @ApiOperation("添加商品到购物车")


    private Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        shoppingCartService.add(shoppingCartDTO);

        return Result.success();

    }
    @GetMapping("/list")
    @ApiOperation("获取购物车列表")
    private Result<List<ShoppingCart>> list(){
        Long userId = (Long) redisTemplate.opsForValue().get("USER_ID");
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(userId);
        return Result.success(shoppingCarts);
    }
    @DeleteMapping("/clean")
    private Result delete(){
        Long userId = (Long) redisTemplate.opsForValue().get("USER_ID");
        shoppingCartService.delete(userId);
        return Result.success();
    }
    @PostMapping("/sub")
    private Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        Long userId = (Long) redisTemplate.opsForValue().get("USER_ID");
        shoppingCartService.sub(userId,shoppingCartDTO);


        return Result.success();
    }
}
